package com.itheima.wemedia.controller;

import com.itheima.admin.pojo.AdChannel;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.dto.WmNewsAuthDto;
import com.itheima.wemedia.dto.WmNewsPageReqDto;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.pojo.WmNews;
import com.itheima.wemedia.pojo.WmUser;
import com.itheima.wemedia.service.ApiService;
import com.itheima.wemedia.service.WmMaterialService;
import com.itheima.wemedia.service.WmNewsService;
import com.itheima.wemedia.service.WmUserService;
import com.itheima.wemedia.vo.WmNewsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class ApiController {
    //注入对象
    @Autowired
    private WmUserService wmUserService;

    @Autowired
    private WmMaterialService wmMaterialService;

    @Autowired
    private ApiService apiService;

    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 远程调用自媒体
     */
    @PostMapping("/api/wmUser/add")
    ResultVo<WmUser> add(@RequestBody WmUser wmUser){
        if (wmUserService.save(wmUser)) {
            return ResultVo.ok(wmUser);
        }
        return ResultVo.error();
    }

    /**
     * 通过appUserId查询自媒体账号的信息
     */
    @GetMapping("/api/wmUser/{appUserId}")
    ResultVo<WmUser> getByAppUserId(@PathVariable Long appUserId){
        WmUser wmUser = wmUserService.getByAppUserId(appUserId);
        return ResultVo.ok(wmUser);
    }

    /**
     * 添加素材
     * @param wmMaterial
     * @return
     */
    @PostMapping("/api/wmMaterial/add")
    ResultVo add(@RequestBody WmMaterial wmMaterial){
        if (wmMaterialService.save(wmMaterial)) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }
    /**
     * 删除素材
     * @param id
     * @return
     */
    @GetMapping("/api/wmMaterial/delete/{id}/{userId}")
    ResultVo delete(@PathVariable Long id,@PathVariable Long userId){
        if (wmMaterialService.delete(id,userId)) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 查询素材
     * @param id
     * @return
     */
    @GetMapping("/api/wmMaterial/select/{id}")
    ResultVo<WmMaterial> selectById(@PathVariable Long id){
        WmMaterial wmMaterial = wmMaterialService.getById(id);
        return ResultVo.ok(wmMaterial);
    }

    /**
     * 查询所有频道
     * @return
     */
    @GetMapping("/channel/channels")
    public ResultVo<List<AdChannel>> listAll(){
        List<AdChannel> adChannelList = apiService.getChannelList();
        return ResultVo.ok(adChannelList);
    }

    /**
     * 通过id查询文章
     * @param newId
     * @return
     */
    @GetMapping("/api/wmNews/{newsId}")
    ResultVo<WmNews> getWmNewsById(@PathVariable(name = "newsId") Integer newId){
        WmNews wmNews = wmNewsService.getById(newId);
        return ResultVo.ok(wmNews);
    }

    /**
     * 通过id更新文章信息
     * @param wmNews
     * @return
     */
    @PostMapping("/api/wmNews/update")
    ResultVo<WmNews> update(@RequestBody WmNews wmNews){
        wmNewsService.updateById(wmNews);
        return ResultVo.ok();
    }

    /**
     * 通过作者名称分页查询
     * @param dto
     * @return
     */
    @PostMapping("/news/list_vo")
    public PageResultVo<WmNewsVo> findPageForAdmin(@RequestBody WmNewsPageReqDto dto){
        PageResultVo<WmNewsVo> pageResultVo = wmNewsService.findPageByAuthorName(dto);
        return pageResultVo;
    }

    /**
     * 文章审核通过--接收参数可以新定义dto接收，这里用map集合接收
     * @param dto
     * @return
     */
    @PostMapping("/news/auth_pass")
    public ResultVo authPassById(@RequestBody WmNewsAuthDto dto){
        wmNewsService.authPass(dto);
        return ResultVo.ok();
    }

    /**
     * 文章审核失败--接收参数可以新定义dto接收，这里用map集合接收
     * @param dto
     * @return
     */
    @PostMapping("/news/auth_fail")
    public ResultVo authFailById(@RequestBody WmNewsAuthDto dto){
        wmNewsService.authFail(dto);
        return ResultVo.ok();
    }

    @GetMapping("/news/one_vo/{newsId}")
    public ResultVo getOneVo(@PathVariable Long newsId){
        WmNewsVo vo = wmNewsService.getOneVo(newsId);
        return ResultVo.ok(vo);
    }

    /**
     * 查询审核通过的文章，状态为8。进行数据同步到文章微服务
     * @param dto
     * @return
     */
    @PostMapping("/api/wmNews/listArticleSync")
    ResultVo<List<WmNewsVo>> listArticleSync(@RequestBody WmNewsPageReqDto dto){
        List<WmNewsVo> newsVoList = wmNewsService.listArticleSync(dto);

        return ResultVo.ok(newsVoList);
    }
}
